package com.youkeyi.ddy.cloud.application.business.hr.approval.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.BusinessApprovalInstanceCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.dto.BusinessApprovalInstanceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.dto.BusinessApprovalInstanceListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.dto.BusinessApprovalNodeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.param.BusinessApprovalInstanceApprovalCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.param.BusinessApprovalInstanceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.approval.instance.param.BusinessApprovalInstanceListCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.instance.HrApprovalInstanceCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.instance.dto.HrApprovalInstanceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.instance.dto.HrApprovalInstanceListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.instance.param.HrApprovalInstanceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.instance.param.HrApprovalInstanceQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.instance.param.HrApprovalInstanceUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.HrApprovalNodeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.dto.HrApprovalNodeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.dto.HrApprovalNodeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.PublicTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.PublicUserCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserQueryCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicHrConstant;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * BusinessApprovalInstanceController
 *
 * @Author liuxiawang
 * @Date 2024/5/15 9:55 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessApprovalInstanceController implements BusinessApprovalInstanceCloudService {

    @Autowired
    private HrApprovalInstanceCloudService hrApprovalInstanceCloudService;
    @Autowired
    private HrApprovalNodeCloudService hrApprovalNodeCloudService;
    @Autowired
    private PublicTodoCloudService publicTodoCloudService;
    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;
    @Autowired
    private PublicUserCloudService publicUserCloudService;

    @Override
    public BusinessApprovalInstanceListCloudDto instanceList(BusinessApprovalInstanceListCloudParam param) {
        BusinessApprovalInstanceListCloudDto dto = new BusinessApprovalInstanceListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrApprovalInstanceQueryCloudParam instanceQueryCloudParam = new HrApprovalInstanceQueryCloudParam();
        instanceQueryCloudParam.setApprovalInstancePrimaryKeys(param.getApprovalInstancePrimaryKeys());
        // instanceQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        instanceQueryCloudParam.setApprovalTypes(param.getApprovalTypes());
        instanceQueryCloudParam.setCreateUserKeys(CollectionUtils.isNotEmpty(param.getCreateUserKeys()) ? param.getCreateUserKeys() : Collections.singletonList(YoukeyiSystemContext.getUserPrimaryKey()));
        instanceQueryCloudParam.setKeyword(param.getKeyword());
        instanceQueryCloudParam.setPageSize(param.getPageSize());
        instanceQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrApprovalInstanceListCloudDto instanceDtos = hrApprovalInstanceCloudService.query(instanceQueryCloudParam);
        if (null != instanceDtos && CollectionUtils.isNotEmpty(instanceDtos.getRows())) {
            // 查询节点
            HrApprovalNodeQueryCloudParam nodeQueryCloudParam = new HrApprovalNodeQueryCloudParam();
            nodeQueryCloudParam.setInstanceKeys(instanceDtos.getRows().stream().map(HrApprovalInstanceListItemCloudDto::getApprovalInstancePrimaryKey).collect(Collectors.toList()));
            HrApprovalNodeListCloudDto nodeDtos = hrApprovalNodeCloudService.query(nodeQueryCloudParam);
            Map<String, List<HrApprovalNodeListItemCloudDto>> nodeMaps = new HashMap<>();
            if (CollectionUtils.isNotEmpty(nodeDtos.getRows())) {
                nodeMaps.putAll(nodeDtos.getRows().stream().collect(Collectors.groupingBy(HrApprovalNodeListItemCloudDto::getInstanceKey)));
            }

            Map<String, String> userMap = new HashMap<>();
            instanceDtos.getRows().forEach(instance -> {
                List<String> userKeys = new ArrayList<>();
                if (StringUtils.isNotBlank(instance.getApproverApproverKey())) {
                    userKeys.addAll(Arrays.asList(instance.getApproverApproverKey().split("\\|")));
                }
                if (StringUtils.isNotBlank(instance.getApproverCcKey())) {
                    userKeys.addAll(Arrays.asList(instance.getApproverCcKey().split("\\|")));
                }
                if (CollectionUtils.isNotEmpty(userKeys)) {
                    PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
                    userQueryCloudParam.setUserPrimaryKeys(userKeys);
                    PublicUserListCloudDto userDtos = publicUserCloudService.query(userQueryCloudParam);
                    if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
                        userMap.putAll(userDtos.getRows().stream().collect(Collectors.toMap(PublicUserListItemCloudDto::getUserPrimaryKey, PublicUserListItemCloudDto::getName, (k1, k2) -> k1)));
                    }
                }
            });

            List<BusinessApprovalInstanceListItemCloudDto> items = new ArrayList<>();
            instanceDtos.getRows().forEach(instance -> {
                BusinessApprovalInstanceListItemCloudDto item = new BusinessApprovalInstanceListItemCloudDto();
                item.setApprovalInstancePrimaryKey(instance.getApprovalInstancePrimaryKey());
                item.setCorpKey(instance.getCorpKey());
                item.setApprovalType(instance.getApprovalType());
                item.setApprovalTypeName(instance.getApprovalTypeName());
                item.setFormJsonStr(instance.getFormJsonStr());
                item.setBusinessKey(instance.getBusinessKey());
                item.setBusinessType(instance.getBusinessType());
                item.setBusinessTypeName(instance.getBusinessTypeName());
                if (StringUtils.isNotBlank(instance.getApproverApproverKey())) {
                    Arrays.asList(instance.getApproverApproverKey().split("\\|")).forEach(key -> item.getApproverApproverName().add(userMap.get(key)));
                }
                if (StringUtils.isNotBlank(instance.getApproverCcKey())) {
                    Arrays.asList(instance.getApproverCcKey().split("\\|")).forEach(key -> item.getApproverCcName().add(userMap.get(key)));
                }
                item.setApproverKey(instance.getApproverKey());
                item.setApprover(instance.getApprover());
                item.setApprovalTime(instance.getApprovalTime());
                item.setApprovalResult(instance.getApprovalResult());
                item.setApprovalComment(instance.getApprovalComment());
                item.setRemarks(instance.getRemarks());
                item.setRemarks(instance.getRemarks());
                item.setIsDeleted(instance.getIsDeleted());
                item.setIsDeletedName(instance.getIsDeletedName());
                item.setCreateTime(instance.getCreateTime());
                item.setCreateUserKey(instance.getCreateUserKey());
                item.setCreateUserName(instance.getCreateUserName());
                item.setUpdateTime(instance.getUpdateTime());
                item.setUpdateUserKey(instance.getUpdateUserKey());
                item.setUpdateUserName(instance.getUpdateUserName());

                if (nodeMaps.containsKey(instance.getApprovalInstancePrimaryKey())) {
                    nodeMaps.get(instance.getApprovalInstancePrimaryKey()).forEach(node -> {
                        BusinessApprovalNodeListItemCloudDto nodeItem = new BusinessApprovalNodeListItemCloudDto();
                        nodeItem.setApprovalNodePrimaryKey(node.getApprovalNodePrimaryKey());
                        nodeItem.setCorpKey(node.getCorpKey());
                        nodeItem.setInstanceKey(node.getInstanceKey());
                        nodeItem.setFormJsonStr(node.getFormJsonStr());
                        nodeItem.setNodeType(node.getNodeType());
                        nodeItem.setSequence(node.getSequence());
                        nodeItem.setApproverKey(node.getApproverKey());
                        if (StringUtils.isNotBlank(nodeItem.getApproverKey())) {
                            Arrays.asList(nodeItem.getApproverKey().split("\\|")).forEach(key -> nodeItem.getApprover().add(userMap.get(key)));
                        }
                        nodeItem.setApprovalTime(node.getApprovalTime());
                        nodeItem.setApprovalResult(node.getApprovalResult());
                        nodeItem.setApprovalComment(node.getApprovalComment());
                        nodeItem.setSuperiorNode(node.getSuperiorNode());
                        nodeItem.setSubordinateNode(node.getSubordinateNode());
                        nodeItem.setRemarks(node.getRemarks());
                        nodeItem.setIsDeleted(node.getIsDeleted());
                        nodeItem.setIsDeletedName(node.getIsDeletedName());
                        nodeItem.setCreateTime(node.getCreateTime());
                        nodeItem.setCreateUserKey(node.getCreateUserKey());
                        nodeItem.setCreateUserName(node.getCreateUserName());
                        nodeItem.setUpdateTime(node.getUpdateTime());
                        nodeItem.setUpdateUserKey(node.getUpdateUserKey());
                        nodeItem.setUpdateUserName(node.getUpdateUserName());
                        item.getNodes().add(nodeItem);
                    });
                }
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(instanceDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent instanceCreate(BusinessApprovalInstanceCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getApprovalType())
                || CollectionUtils.isEmpty(param.getApproverKeys())
                || CollectionUtils.isEmpty(param.getCcKeys())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrApprovalInstanceCreateCloudParam instanceCreateCloudParam = new HrApprovalInstanceCreateCloudParam();
        instanceCreateCloudParam.setCorpKey(corpKey);
        instanceCreateCloudParam.setApprovalType(param.getApprovalType());
        instanceCreateCloudParam.setFormJsonStr(param.getFormJsonStr());
        instanceCreateCloudParam.setBusinessKey(param.getBusinessKey());
        instanceCreateCloudParam.setBusinessType(param.getBusinessType());
        instanceCreateCloudParam.setApproverApproverKey(String.join("|", param.getApproverKeys()));
        instanceCreateCloudParam.setApproverCcKey(String.join("|", param.getCcKeys()));
        instanceCreateCloudParam.setRemarks(param.getRemarks());
        instanceCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        instanceCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        for (int i = 0; i < param.getApproverKeys().size(); i++) {
            HrApprovalNodeCreateCloudParam nodeCreateCloudParam = new HrApprovalNodeCreateCloudParam();
            nodeCreateCloudParam.setApprovalNodePrimaryKey(SimpleUUidUtils.getUuid());
            nodeCreateCloudParam.setNodeType(YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_NODE_TYPE_APPROVAL.value());
            nodeCreateCloudParam.setSequence(i);
            nodeCreateCloudParam.setApproverKey(param.getApproverKeys().get(i));
            nodeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            nodeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            instanceCreateCloudParam.getNodes().add(nodeCreateCloudParam);
        }
        if (CollectionUtils.isNotEmpty(param.getCcKeys())) {
            HrApprovalNodeCreateCloudParam nodeCreateCloudParam = new HrApprovalNodeCreateCloudParam();
            nodeCreateCloudParam.setApprovalNodePrimaryKey(SimpleUUidUtils.getUuid());
            nodeCreateCloudParam.setNodeType(YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_NODE_TYPE_CC.value());
            nodeCreateCloudParam.setSequence(param.getApproverKeys().size() + 1);
            nodeCreateCloudParam.setApproverKey(String.join("|", param.getCcKeys()));
            nodeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            nodeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            instanceCreateCloudParam.getNodes().add(nodeCreateCloudParam);
        }
        rc = hrApprovalInstanceCloudService.create(instanceCreateCloudParam);
        if (null != rc && rc.isSuccess()) {
            // 创建节点的第一个代办
            if (CollectionUtils.isNotEmpty(instanceCreateCloudParam.getNodes())) {
                HrApprovalNodeCreateCloudParam node = instanceCreateCloudParam.getNodes().get(0);

                // 创建审批类型的代办
                PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
                todoCreateCloudParam.setUserKey(node.getApproverKey());
                todoCreateCloudParam.setType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_TODO_TYPE_APPROVAL_APPROVAL.value());
                todoCreateCloudParam.setCustomerKey(node.getApprovalNodePrimaryKey()); // 这里存放的就是node key了
                todoCreateCloudParam.setContent(String.format("%s审批", YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE.getName(param.getApprovalType())));
                todoCreateCloudParam.setStatus(1);
                todoCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                todoCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
                publicTodoCloudService.create(todoCreateCloudParam);
            }

            // 修改员工状态
            if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_EMPLOYMENT.value().equals(param.getApprovalType())) {
                // 入职审批，把员工改成 审核中
                HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                employeeUpdateCloudParam.setEmployeePrimaryKey(param.getBusinessKey());
                employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_3.value());
                hrEmployeeCloudService.update(employeeUpdateCloudParam);
            }
        }
        return rc;
    }

    @Override
    public ResponseContent approval(BusinessApprovalInstanceApprovalCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getApprovalNodePrimaryKey())
                || StringUtils.isBlank(param.getApprovalResult())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        // 查询节点信息并做校验
        HrApprovalNodeQueryCloudParam nodeQueryCloudParam = new HrApprovalNodeQueryCloudParam();
        nodeQueryCloudParam.setApprovalNodePrimaryKeys(Collections.singletonList(param.getApprovalNodePrimaryKey()));
        HrApprovalNodeListCloudDto nodeDtos = hrApprovalNodeCloudService.query(nodeQueryCloudParam);
        if (CollectionUtils.isEmpty(nodeDtos.getRows())) {
            rc.setErrorMessage(ResponseCodeEnum.HR_APPROVAL_NODE_QUERY_ERROR.getMessage());
            return rc;
        }
        // 校验是否本人
        HrApprovalNodeListItemCloudDto node = nodeDtos.getRows().get(0);
        if (!node.getApproverKey().equals(YoukeyiSystemContext.getUserPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.HR_APPROVAL_NODE_APPROVAL_ERROR.getMessage());
            return rc;
        }
        // 查询审批实例信息
        HrApprovalInstanceQueryCloudParam instanceQueryCloudParam = new HrApprovalInstanceQueryCloudParam();
        instanceQueryCloudParam.setApprovalInstancePrimaryKeys(Collections.singletonList(node.getInstanceKey()));
        HrApprovalInstanceListCloudDto instanceDtos = hrApprovalInstanceCloudService.query(instanceQueryCloudParam);
        if (null == instanceDtos || CollectionUtils.isEmpty(instanceDtos.getRows())) {
            rc.setErrorMessage(ResponseCodeEnum.HR_APPROVAL_NODE_QUERY_ERROR.getMessage());
            return rc;
        }
        HrApprovalInstanceListItemCloudDto instance = instanceDtos.getRows().get(0);

        // 修改当前节点，保存审批结果
        HrApprovalNodeUpdateCloudParam nodeUpdateCloudParam = new HrApprovalNodeUpdateCloudParam();
        nodeUpdateCloudParam.setApprovalNodePrimaryKey(node.getApprovalNodePrimaryKey());
        nodeUpdateCloudParam.setApprover(YoukeyiSystemContext.getUserName());
        nodeUpdateCloudParam.setApprovalTime(System.currentTimeMillis());
        nodeUpdateCloudParam.setApprovalResult(param.getApprovalResult());
        nodeUpdateCloudParam.setApprovalComment(param.getApprovalComment());
        nodeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        nodeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        rc = hrApprovalNodeCloudService.update(nodeUpdateCloudParam);
        if (null != rc && rc.isSuccess()) {
            if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_RESULT_REJECT.value().equals(param.getApprovalResult())) {
                // 如果是审批失败的话，结束整个审批实例
                HrApprovalInstanceUpdateCloudParam instanceUpdateCloudParam = new HrApprovalInstanceUpdateCloudParam();
                instanceUpdateCloudParam.setApprovalInstancePrimaryKey(node.getApprovalNodePrimaryKey());
                instanceUpdateCloudParam.setApproverKey(YoukeyiSystemContext.getUserPrimaryKey());
                instanceUpdateCloudParam.setApprover(YoukeyiSystemContext.getUserName());
                instanceUpdateCloudParam.setApprovalTime(System.currentTimeMillis());
                instanceUpdateCloudParam.setApprovalResult(param.getApprovalResult());
                instanceUpdateCloudParam.setApprovalComment(param.getApprovalComment());
                instanceUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                instanceUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
                hrApprovalInstanceCloudService.update(instanceUpdateCloudParam);

                // 修改员工状态
                if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_EMPLOYMENT.value().equals(instance.getApprovalType())) {
                    // 入职审批，把员工改成 审核不通过
                    HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                    employeeUpdateCloudParam.setEmployeePrimaryKey(instance.getBusinessKey());
                    employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_12.value());
                    hrEmployeeCloudService.update(employeeUpdateCloudParam);
                }
            }
            else if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_RESULT_AGREE.value().equals(param.getApprovalResult())) {
                // 如果是审批同意的话，判断是否是最后一个节点
                HrApprovalNodeQueryCloudParam nextNodeQueryCloudParam = new HrApprovalNodeQueryCloudParam();
                nextNodeQueryCloudParam.setInstanceKeys(Collections.singletonList(node.getInstanceKey()));
                nextNodeQueryCloudParam.setSequence(node.getSequence() + 1);
                HrApprovalNodeListCloudDto nextNodeDtos = hrApprovalNodeCloudService.query(nextNodeQueryCloudParam);
                if (CollectionUtils.isEmpty(nextNodeDtos.getRows())) {
                    // 没有下个节点了，结束整个审批实例
                    HrApprovalInstanceUpdateCloudParam instanceUpdateCloudParam = new HrApprovalInstanceUpdateCloudParam();
                    instanceUpdateCloudParam.setApprovalInstancePrimaryKey(node.getApprovalNodePrimaryKey());
                    instanceUpdateCloudParam.setApproverKey(YoukeyiSystemContext.getUserPrimaryKey());
                    instanceUpdateCloudParam.setApprover(YoukeyiSystemContext.getUserName());
                    instanceUpdateCloudParam.setApprovalTime(System.currentTimeMillis());
                    instanceUpdateCloudParam.setApprovalResult(param.getApprovalResult());
                    instanceUpdateCloudParam.setApprovalComment(param.getApprovalComment());
                    instanceUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                    instanceUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
                    hrApprovalInstanceCloudService.update(instanceUpdateCloudParam);

                    // 修改员工状态
                    if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_EMPLOYMENT.value().equals(instance.getApprovalType())) {
                        // 入职审批，审核通过后，把员工改成 待确认入职
                        HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                        employeeUpdateCloudParam.setEmployeePrimaryKey(instance.getBusinessKey());
                        employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_13.value());
                        hrEmployeeCloudService.update(employeeUpdateCloudParam);
                    } else if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_RESIGNATION.value().equals(instance.getApprovalType())) {
                        // 离职审批，审核通过后 把员工改成 离职
                        HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                        employeeUpdateCloudParam.setEmployeePrimaryKey(instance.getBusinessKey());
                        employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_10.value());
                        hrEmployeeCloudService.update(employeeUpdateCloudParam);
                        // 删除账号
                        PublicUserDeleteCloudParam userDeleteCloudParam = new PublicUserDeleteCloudParam();
                        userDeleteCloudParam.setUserPrimaryKeys(Collections.singletonList(instance.getBusinessKey()));
                        userDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                        userDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
                        publicUserCloudService.delete(userDeleteCloudParam);
                    }
                } else {
                    // 还有下个节点，给下个节点生成代办
                    HrApprovalNodeListItemCloudDto nextNode = nextNodeDtos.getRows().get(0);

                    // 看看节点类型是审批还是抄送
                    if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_NODE_TYPE_APPROVAL.value().equals(nextNode.getNodeType())) {
                        // 节点类型是审批的，创建审批类型的代办
                        PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
                        todoCreateCloudParam.setUserKey(nextNode.getApproverKey());
                        todoCreateCloudParam.setType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_TODO_TYPE_APPROVAL_APPROVAL.value());
                        todoCreateCloudParam.setCustomerKey(nextNode.getApprovalNodePrimaryKey()); // 这里存放的就是node key了
                        todoCreateCloudParam.setContent(String.format("%s审批", YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE.getName(instance.getApprovalType())));
                        todoCreateCloudParam.setStatus(1);
                        todoCreateCloudParam.setCreateUserKey("SYSTEM");
                        todoCreateCloudParam.setCreateUserName("SYSTEM");
                        publicTodoCloudService.create(todoCreateCloudParam);
                    } else if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_NODE_TYPE_CC.value().equals(nextNode.getNodeType())) {
                        // 节点类型是cc的话
                        Arrays.asList(nextNode.getApprover().split("\\|")).forEach(approverKey -> {
                            // 节点类型是审批的，创建审批类型的代办
                            PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
                            todoCreateCloudParam.setUserKey(approverKey);
                            todoCreateCloudParam.setType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_TODO_TYPE_APPROVAL_CC.value());
                            todoCreateCloudParam.setCustomerKey(nextNode.getApprovalNodePrimaryKey()); // 这里存放的就是node key了
                            todoCreateCloudParam.setContent(String.format("%s审批", YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE.getName(instance.getApprovalType())));
                            todoCreateCloudParam.setStatus(1);
                            todoCreateCloudParam.setCreateUserKey("SYSTEM");
                            todoCreateCloudParam.setCreateUserName("SYSTEM");
                            publicTodoCloudService.create(todoCreateCloudParam);
                        });

                        // 修改员工状态
                        if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_EMPLOYMENT.value().equals(instance.getApprovalType())) {
                            // 入职审批，抄送就是最后一个节点了。审核通过后，把员工改成 待确认入职
                            HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                            employeeUpdateCloudParam.setEmployeePrimaryKey(instance.getBusinessKey());
                            employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_13.value());
                            hrEmployeeCloudService.update(employeeUpdateCloudParam);
                        } else if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_RESIGNATION.value().equals(instance.getApprovalType())) {
                            // 离职审批，抄送就是最后一个节点了。审核通过后 把员工改成 离职
                            HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                            employeeUpdateCloudParam.setEmployeePrimaryKey(instance.getBusinessKey());
                            employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_10.value());
                            hrEmployeeCloudService.update(employeeUpdateCloudParam);
                            // 删除账号
                            PublicUserDeleteCloudParam userDeleteCloudParam = new PublicUserDeleteCloudParam();
                            userDeleteCloudParam.setUserPrimaryKeys(Collections.singletonList(instance.getBusinessKey()));
                            userDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                            userDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
                            publicUserCloudService.delete(userDeleteCloudParam);
                        }
                    }
                }
            }
            else {
                rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
                return rc;
            }
            rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        }
        return rc;
    }


    @Override
    public ResponseContent revoke(BusinessApprovalInstanceApprovalCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || StringUtils.isBlank(param.getApprovalPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        // 查询审批实例信息
        HrApprovalInstanceQueryCloudParam instanceQueryCloudParam = new HrApprovalInstanceQueryCloudParam();
        instanceQueryCloudParam.setApprovalInstancePrimaryKeys(Collections.singletonList(param.getApprovalPrimaryKey()));
        HrApprovalInstanceListCloudDto instanceDtos = hrApprovalInstanceCloudService.query(instanceQueryCloudParam);
        if (null == instanceDtos || CollectionUtils.isEmpty(instanceDtos.getRows())) {
            rc.setErrorMessage(ResponseCodeEnum.HR_APPROVAL_NODE_QUERY_ERROR.getMessage());
            return rc;
        }
        HrApprovalInstanceListItemCloudDto instance = instanceDtos.getRows().get(0);

        // 撤销整个实例
        HrApprovalInstanceUpdateCloudParam instanceUpdateCloudParam = new HrApprovalInstanceUpdateCloudParam();
        instanceUpdateCloudParam.setApprovalInstancePrimaryKey(instance.getApprovalInstancePrimaryKey());
        instanceUpdateCloudParam.setApproverKey(YoukeyiSystemContext.getUserPrimaryKey());
        instanceUpdateCloudParam.setApprover(YoukeyiSystemContext.getUserName());
        instanceUpdateCloudParam.setApprovalTime(System.currentTimeMillis());
        instanceUpdateCloudParam.setApprovalResult(YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_RESULT_REVOKE.value()); // 撤销
        instanceUpdateCloudParam.setApprovalComment(param.getApprovalComment());
        instanceUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        instanceUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        rc = hrApprovalInstanceCloudService.update(instanceUpdateCloudParam);
        if (null != rc && rc.isSuccess()) {
            // 修改节点状态为取消
            HrApprovalNodeQueryCloudParam nodeQueryCloudParam = new HrApprovalNodeQueryCloudParam();
            nodeQueryCloudParam.setInstanceKeys(Collections.singletonList(instance.getApprovalInstancePrimaryKey()));
            HrApprovalNodeListCloudDto nodeDtos = hrApprovalNodeCloudService.query(nodeQueryCloudParam);
            if (nodeDtos != null && CollectionUtils.isNotEmpty(nodeDtos.getRows())) {
                nodeDtos.getRows().forEach(node -> {
                    if (StringUtils.isBlank(node.getApprovalResult())) {
                        // 修改当前节点，保存审批结果
                        HrApprovalNodeUpdateCloudParam nodeUpdateCloudParam = new HrApprovalNodeUpdateCloudParam();
                        nodeUpdateCloudParam.setApprovalNodePrimaryKey(node.getApprovalNodePrimaryKey());
                        nodeUpdateCloudParam.setApprover(YoukeyiSystemContext.getUserName());
                        nodeUpdateCloudParam.setApprovalTime(System.currentTimeMillis());
                        nodeUpdateCloudParam.setApprovalResult(YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_RESULT_REVOKE.value());
                        nodeUpdateCloudParam.setApprovalComment("该审批被撤销");
                        nodeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                        nodeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
                        hrApprovalNodeCloudService.update(nodeUpdateCloudParam);
                    }
                });
            }

            // 修改员工状态
            if (YoukeyiDdyCloudBasicHrConstant.HR_APPROVAL_TYPE_EMPLOYMENT.value().equals(instance.getApprovalType())) {
                // 入职审批，撤销之后 改状态为待审核
                HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
                employeeUpdateCloudParam.setEmployeePrimaryKey(instance.getBusinessKey());
                employeeUpdateCloudParam.setEmployeeStatus(YoukeyiDdyCloudBasicHrConstant.HR_EMPLOYEE_STATUS_2.value());
                hrEmployeeCloudService.update(employeeUpdateCloudParam);
            }
        }
        return rc;
    }
}
